<?php


/************************ Theme Initial setup ***********************************/

add_action ('after_setup_theme','thinlines_theme_setup');


function thinlines_theme_setup () {

add_theme_support('post-thumbnails');
set_post_thumbnail_size(150, 150, true);
add_theme_support( 'automatic-feed-links' );

if ( ! isset( $content_width ) )
	$content_width = 1115;
	

$thinlines_settings = get_option('thinlines-options');	

update_option('thinlines-options', $thinlines_settings);

	
}	


/******************** Initial theme default values *********************************/

function thinlines_defaults() {
	
$defaults = array (
	
	'logo' => '',
	'favicon' => '',
	'footer' => '',
	'comments' => '1',
	'showcase' => '1',
	'showcase-image' => '',
	'showcase-title' => '',
	'showcase-text' => '',
	'showcase-color' => '#39ad94'
	);

return $defaults;
	
}


	
/********************** Adjust content width for full page templates *******************************/

function thinlines_content_width() {
	
	global $content_width;
	
	$thinlines_settings = get_option('thinlines-options');
	
	if(isset($thinlines_settings['fullcontent']) && $thinlines_settings['fullcontent'] =='1')
			$content_width = 1115;

	
	if(is_page_template( 'fullwidth-page.php' ) ) 
		$content_width = 1115;
			
	
}

add_action( 'template_redirect', 'thinlines_content_width' ); 


/********************************* Excerpts fix *******************************/

add_filter('excerpt_more', 'thinlines_new_excerpt_more');

function thinlines_new_excerpt_more( $more ) {
	return '';
}

/**************************** Theme Filter Excerpts **********************************/

add_filter('excerpt_more', 'thinlines_excerpt_remove');

function thinlines_excerpt_remove( $more ) {
	return ' <p><a class="more-link" href="'. get_permalink( get_the_ID() ) . '">Continue Reading &raquo;</a></p>';
}


/*************** Display content or user trimmed/auto excerpt  *******************************/

function thinlines_excerpt_content() {

 if( strpos( get_the_content(), 'more-link' ) === false ) {
                    the_excerpt();
                }
                else {
                    the_content('Continue Reading &raquo;');
                }	
	
}

/***************************** Load Admin Theme Options Scripts *************************/

add_action( 'admin_enqueue_scripts', 'thinlines_admin_scripts' );

function thinlines_admin_scripts($hook) {
	
	if('appearance_page_tl-theme-options' != $hook) {
		return;
	}

	 wp_enqueue_script('jquery');
	 wp_enqueue_style( 'wp-color-picker' );
	 wp_enqueue_script( 'tl-color-script', get_template_directory_uri().'/js/colorpicker.js', array( 'wp-color-picker','jquery' ), false, true );
     
     
   	wp_enqueue_script('media-upload');
	wp_enqueue_script('thickbox');
	wp_enqueue_style('thickbox');
	wp_enqueue_script('uploader', get_template_directory_uri().'/js/uploader.js');
}


/************************* Theme Custom Colors ************************************/

function thinlines_custom_styles() {
	
	
	$thinlines_options = get_option('thinlines-options');
	
	echo '<style>#header, .headercontent { background:'. esc_html($thinlines_options['showcase-color']) .' !important }</style>';
	
	
	
}

add_action('wp_head','thinlines_custom_styles');


/********************************** Theme Setup scripts, Fonts and styles ************************************/

function thinlines_scripts_styles()
{
	

	if ( is_singular() && comments_open() && get_option( 'thread_comments' ) )
		wp_enqueue_script( 'comment-reply' );
	
	wp_register_style('thinlines-fonts','//fonts.googleapis.com/css?family=Fjalla+One|Open+Sans:300');
	
	wp_enqueue_style( 'thinlines-css', get_stylesheet_uri() ); // default stylesheet
	wp_enqueue_style( 'thinlines-fonts');
	

}

add_action ('wp_enqueue_scripts','thinlines_scripts_styles');	



/******************************** Register Navigation menus ***************************/
 
add_action( 'init', 'thinlines_nav_menu' );

 function thinlines_nav_menu() {
    register_nav_menu( 'primary-menu',  'Primary Menu');
    register_nav_menu( 'top-menu', 'Top Menu' );
    register_nav_menu( 'footer-menu', 'Footer Menu' );
}


/************************** Theme Pagination *************************************/

function thinlines_pagination() {
		global $wp_query;

		$big = 999999999; // need an unlikely integer
		
		echo paginate_links( array(
			'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
			'format' => '?paged=%#%',
			'current' => max( 1, get_query_var('paged') ),
			'total' => $wp_query->max_num_pages
		) );
	}
	
	
/******************* Post Pagination using Next tag ************************************/	
	
function thinlines_wp_link_pages() {

return wp_link_pages(array('before' => '<div id="page-links"> Page Sections:  ','after' => '</div>','next_or_number'=>'number', 'previouspagelink' => '&laquo;  Previous Page', 'nextpagelink'=>'Next Page &raquo;'));	
	
}	

	
/********************************* Theme Widgets Setup **********************************/

add_action( 'widgets_init', 'thinlines_widgets_init' );

function thinlines_widgets_init() {
	
/* Right Sidebars */	
	register_sidebar(array(
  'name' =>  'Right Hand Sidebar',
  'id' => 'right-sidebar',
  'description' => 'Widgets in this area will be shown on the right-hand side.',
  'before_title' => '<h3>',
  'after_title' => '</h3>'
));



/* Top Widget */

register_sidebar(array(
  'name' =>  'Top Widget',
  'id' => 'top-widget',
  'description' => 'Appears below navigation menu with featured content.',
  'before_title' => '<h3>',
  'after_title' => '</h3>'
));

}	



/******************************** Filter the Title *******************************/

function thinlines_wp_title( $title, $sep ) {
	global $paged, $page;

	if ( is_feed() )
		return $title;

	// Add the site name.
	$title .= get_bloginfo( 'name' );

	// Add the site description for the home/front page.
	$site_description = get_bloginfo( 'description', 'display' );
	if ( $site_description && ( is_home() || is_front_page() ) )
		$title = "$title $sep $site_description";

	// Add a page number if necessary.
	if ( $paged >= 2 || $page >= 2 )
		$title = "$title $sep " . sprintf( 'Page %s', max( $paged, $page ) );

	return $title;
}
add_filter( 'wp_title', 'thinlines_wp_title', 10, 2 );



/********************************* Theme Admin section *******************************************/

if (is_admin()) {
add_action ('admin_menu', 'thinlines_admin_menu');
}

function thinlines_admin_menu() {
	
	add_theme_page( 'Theme Option','thinlines Options', 'edit_theme_options', 'tl-theme-options', 'thinlines_options_page'); 
	add_action( 'admin_init','thinlines_register_mysettings' );

}

function thinlines_register_mysettings() {
  register_setting( 'thinlines-option-group', 'thinlines-options' ,'thinlines_options_validate');
}


/************************ Validation of options page input data ***********************/

function thinlines_options_validate ($input) {

$input['logo'] = esc_url_raw($input['logo']);
$input['favicon'] = esc_url_raw($input['favicon']);
$input['showcase-image'] = esc_url_raw($input['showcase-image']);
$input['showcase-title'] = sanitize_text_field($input['showcase-title']);
$input['showcase-text'] = sanitize_text_field($input['showcase-text']);
$input['showcase-color'] = sanitize_text_field($input['showcase-color']);

return $input;

}

/****************************** Theme Options Page *********************************/

function thinlines_options_page() {
?>	
	<div class="wrap">
<h2>Thinlines Theme Settings</h2>
<form method="post" action="options.php"> 
<?php 
settings_fields( 'thinlines-option-group' );
do_settings_sections( 'thinlines-option-group' );	
				

// Reset default values
if(isset($_POST['reset'])) {
	update_option('thinlines-options', thinlines_defaults() );
	echo '<p style="color:red">Theme settings have been reset and default values loaded</p>';
}

$thinlines_options = get_option('thinlines-options');


?>



           <p>Please upload the logo and favicon to media library and paste the URL below.</p> 
Logo<br><input type="text" size="50" id="logo_image" name="thinlines-options[logo]" value="<?php echo esc_url($thinlines_options['logo']); ?>"> 
<input id="logo_button" type="button" value="Upload Image" /> (200x90px - only transparent PNG) 
<br>
Favicon <br><input size="50" id="favicon_image" type="text" name="thinlines-options[favicon]" value="<?php echo esc_url($thinlines_options['favicon']); ?>"><input id="favicon_button" type="button" value="Upload Image" /> <br>
Footer Copyright Text <br><input type="text" size="50" name="thinlines-options[footer]" value="<?php echo esc_attr($thinlines_options['footer']); ?>"><br><br>

<h2>Comment Settings</h2>
<p>You can chose to completely disable comments and comment form in this theme. If you receive spam and hate comments, disable it. Enabled by default.</p>
<p><input type="checkbox" name="thinlines-options[comments]" value="1" <?php if(isset($thinlines_options['comments'])) { checked(absint($thinlines_options['comments']),1); }?>> Enable/Disable Comments in Posts/Pages <p>



           
<h2>Featured Box Settings </h2>
If no value exists, the theme will auto hide the elements. It will only show if values exists. 
<p><input type="checkbox" name="thinlines-options[showcase]" value="1" <?php if(isset($thinlines_options['showcase'])) { checked(absint($thinlines_options['showcase']),1); }?>> Enable/Disable Showcasebox Box <p>

Showcase Image:<br><input id="showcase_image" type="text" size="50" name="thinlines-options[showcase-image]" value="<?php echo esc_url($thinlines_options['showcase-image']); ?>"><input id="showcase_button" type="button" value="Upload Image" /> (big sizes like 300x300 or more)<br><br>
Showcase Title: <br><input type="text" size="50" name="thinlines-options[showcase-title]" value="<?php echo esc_attr($thinlines_options['showcase-title']); ?>"><br><br>
Showcase Text <br><textarea rows="4" cols="50" name="thinlines-options[showcase-text]"><?php echo esc_textarea($thinlines_options['showcase-text']); ?></textarea><br><br>

<h2>Header Background Color</h2>
<p>Pick background color. Makes changes to background color of header and featured box</p> 
<input type="text" name="thinlines-options[showcase-color]" class='color-picker' id="color-picker-8"
style="background: <?php echo esc_attr($thinlines_options['showcase-color']);  ?>" 
value="<?php echo esc_attr($thinlines_options['showcase-color']);  ?>" /></p>
							


<?php submit_button(); ?> 
</form>
<h2>Reset Defaults</h2>
<form method="post" action="">
	<p class="submit">
	Load theme default settings: <input name="reset" class="button button-secondary"  type="submit" value="Reset to theme default settings" >
		<input type="hidden" name="action" value="reset" />
	</p>
</form>
</div>

<?php
}
?>